﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.DAL;
using HsTec.Mobile.Green.Model;

public partial class ReportStorage_CitiesInformation : System.Web.UI.Page
{
    ReportTemplateDAL dal = new ReportTemplateDAL();
    ReportTemplateBLL bll = new ReportTemplateBLL();
    ReportAnalysisBLL rabll = new ReportAnalysisBLL();
    private string sAreaId = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        sAreaId = (string)Session["Baimasoft_Report"];
        lstArea.Attributes.Add("onclick", "FindSelectedItems(this," + txtArea.ClientID + ");");
        if (!IsPostBack)
        {
            sltsYear.Value = DateTime.Now.Year.ToString();
            sltsMonth.Value = string.Format("{0:D2}", DateTime.Now.Month);
            slteYear.Value = DateTime.Now.Year.ToString();
            slteMonth.Value = string.Format("{0:D2}", DateTime.Now.Month);
            this.sltTablebind();
            this.BindlstArea();
        }
    }
    protected void sltTable_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(sltTable.SelectedValue.Trim()))
        {
            sltField.DataSource = dal.GetList("FieldParent='" + sltTable.SelectedValue.Trim() + "' and FieldChildCount>0");
            sltField.DataTextField = "FieldAlias";
            sltField.DataValueField = "RT_ID";
            sltField.DataBind();
            sltField.Items.Insert(0, "请选择...");
            sltField.Items[0].Value = "";
        }
    }
    private void sltTablebind()
    {
        sltTable.DataSource = dal.GetList("FieldParent=0");
        sltTable.DataTextField = "FieldAlias";
        sltTable.DataValueField = "RT_ID";
        sltTable.DataBind();
        sltTable.Items.Insert(0, "请选择...");
        sltTable.Items[0].Value = "";
    }

    /// <summary>
    /// 绑定区域数据
    /// </summary>
    private void BindlstArea()
    {
        lstArea.DataSource = rabll.GetAreaList(sAreaId);
        lstArea.DataTextField = "Name";
        lstArea.DataValueField = "ID";
        lstArea.DataBind();
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        GridViewBind();
    }

    private void GridViewBind()
    {
        if ((sltField.Items.Count == 1 && sltTable.SelectedValue != "") || sltField.Value != "")
        {
            string sWhere = String.Empty;
            sWhere = this.getQueryString();
            GridView1.DataSource = bll.getGridViewInfo(sltTable.SelectedValue, sltField.Value, sWhere);
            GridView1.DataBind();
            HiddensID.Value = "";
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择数据表和相应子项！');</script>");
        }
    }

    private string getQueryString()
    {
        string sArea = String.Empty;
        string searchStr = " where (1=1)";
        //获取地区信息
        for (int i = 0; i < lstArea.Items.Count; i++)
        {
            if (lstArea.Items[i].Selected == true)
            {
                sArea += sArea == String.Empty ? lstArea.Items[i].Value : "," + lstArea.Items[i].Value;
            }
        }
        if (!String.IsNullOrEmpty(sArea))
            searchStr += " and Area_ID in (" + sArea + ")";
        //获取日期信息
        searchStr += "and Years between '" + sltsYear.Value + sltsMonth.Value + "' and '" + slteYear.Value + slteMonth.Value + "'";
        return searchStr;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            e.Row.Cells[0].Text = "<input id='chks" + e.Row.Cells[0].Text + "' type='checkbox' onclick='javascript:GetId(this);' value='" + e.Row.Cells[0].Text + "'/>";
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = "<input id='chks" + e.Row.Cells[0].Text + "' type='checkbox' onclick='javascript:CheckAll(this);'/>全选";
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (HiddensID.Value != "")
        {
            string table = String.Empty;
            if (sltField.Value == "" && sltTable.SelectedValue != "")
            {
                table = sltTable.SelectedValue;
            }
            else
            {
                table = sltField.Value;
            }
            bll.DeleteInfo(table, HiddensID.Value);
            HiddensID.Value = "";
            GridViewBind();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功！');</script>");
        }
    }
}
